package com.zw.pdm.module.file.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.FileSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.file.service.FileListService;
import com.zw.pdm.module.file.service.FileSignForService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划确认接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/file/signFor")
@SuppressWarnings("all")
public class FileSignForController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private FileListService fileListService;
    @Autowired
    private FileSignForService fileSignForService;


    @GetMapping
    @RequiresPermissions(value = {"file:sign:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "file/signfor/signForList";
    }

    /** 查询需要签收的存档列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions(value = {"file:sign:view"}, logical = Logical.OR)
    public JsonResult list(FileSheet fileSheet) {
        startPage();
        List<FileSheet> list = fileSignForService.findFileSignFor(fileSheet);
        return jsonResult(list);
    }

    /** 查看页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions(value = {"file:sign:view"}, logical = Logical.OR)
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/filePrint";

    }

    /** 下发页面 */
    @GetMapping("/check/{id}")
    @RequiresPermissions(value = {"file:sign:view"}, logical = Logical.OR)
    public String check(@PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/signfor/fileSign";

    }

    /** 签收存档 */
    @ResponseBody
    @PostMapping("/y")
    @RequiresPermissions(value = {"file:sign:qscd"}, logical = Logical.OR)
    public JsonResult yes(Integer id) {
        return isNotNull(fileSignForService.signConfirm(id));
    }

    /** 拒收存档 */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions(value = {"file:sign:qscd"}, logical = Logical.OR)
    public JsonResult reject(FileSheet fileSheet) {
        return isNotNull(fileSignForService.signReject(fileSheet));

    }

    /** 编辑档案备注 */
    @ResponseBody
    @PostMapping("/modify")
    @RequiresPermissions(value = {"file:sign:view"}, logical = Logical.OR)
    public JsonResult modify(FileSheet fileSheet) {
        return isNotNull(fileSignForService.modifyFile(fileSheet));
    }

}
